<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Admin\RecommendController;
use DB;
use Illuminate\Http\Request;
use EasyWeChat\Foundation\Application;
use App\Http\Controllers\Controller;

use App\Http\Models\Post;
use App\Http\Models\PostCid;
use App\Http\Models\Menu;
use App\Http\Models\Config;
use App\Http\Models\Banner;
use App\Http\Models\Top;
use App\Http\Models\Ad;
use App\Http\Models\Recommend;
use App\Http\Models\Friend;

class QueryController extends Controller {

    //缓存
    static $pool = [];

    public function postsCat(){
        $category = PostCid::get();
        return $category;
    }

    public function menus($position = 1)
    {
        return $this->getMenuList($position);

    }

    private function getMenuList($position){
        $all = Menu::where('position',$position)->get()->toArray();
        $menus = [];

        foreach($all as $key => $val){
            $menus[$val['id']] = $val;
        }

        $menus = $this->listMenuToTree($menus);
        return $menus;
    }

    //不是真正的树 树的效率没有这个高 本方法只需要遍历一遍 复杂度O(n)
    private function listMenuToTree( $listMenu )
    {
        foreach( $listMenu as $key => $val ){
            if(isset($listMenu[$val['pid']])){
                if(!@is_array($listMenu[$val['pid']]['sub'])) {
                    $listMenu[$val['pid']]['sub'] = array();
                }
                array_push($listMenu[$val['pid']]['sub'],$val);
            }
        }
        return $listMenu;
    }


    /**
     * 通过文章分类id直接找
     * @param $cid
     * @return mixed
     */
    public function getCid($cid){
        if(isset(self::$pool['cid'][$cid])) return self::$pool['cid'][$cid];
        else{
            return self::$pool['cid'][$cid] = PostCid::find($cid);
        }
    }

    /**
     * 通过文章id找
     */
    public function getCidByAid($aid){
        if(isset(self::$pool['aidtocid'][$aid])) return self::$pool['aidtocid'][$aid];
        else{
            $post = Post::where('id',$aid)->first();
            return self::$pool['aidtocid'][$aid] = PostCid::find($post->cid);
        }
    }

    public function config($key)
    {

        if(isset(self::$pool['config'][$key])) return self::$pool['config'][$key];
        else{
            $config =  Config::where('ckey',$key)->first();;
            if($config) return self::$pool['config'][$key] = $config->cval;
            else return '[]';
        }
    }

    public function getBannerList($num = 5)
    {
        if(isset(self::$pool['banner'][$num]))  return self::$pool['banner'][$num];
        else return self::$pool['banner'][$num] =  Banner::all()->take($num);
    }


    /**
     * 获取文章点击排行榜
     * @param int $type 1 周  2 月
     * @return mixed
     */
    public function getTop($type=1){
        if(isset(self::$pool['top'][$type])) return self::$pool['top'][$type];
        else {
            $time = time();
            $year = date('Y',$time);
            $month = date("m",$time);
            $week = date('W',$time);
            if(1 == $type){
                return self::$pool['top'][$type] = Top::where(['year'=>$year,'week'=>$week])->orderBy("pv",'desc')->take(10)->get();
            }else if(2 == $type){
                return self::$pool['top'][$type] = Top::where(['year'=>$year,'month'=>$month])->orderBy("pv",'desc')->take(10)->get();
            }
            return [];
        }
    }

    /**
     * 获取广告
     * @param $id
     */
    public function getAd($position)
    {

        if(isset(self::$pool['ad'][$position])) return self::$pool['ad'][$position];
        else{
            return self::$pool['ad'][$position] = Ad::where('position',$position)->get()->take(1);
        }
    }


    /**
     * 根据位置获取推荐文章
     * @param $position
     * @param int $num
     * @return array
     */
    public function getRec($position,$num=6){
        if(isset(self::$pool['rec'][$position][$num])) return self::$pool['rec'][$position][$num];
        else{
            $posts = Recommend::where('position',$position)->first();
            if(!$posts) return [];
            if($posts = json_decode($posts->assoc,true)){
                return self::$pool['rec'][$position][$num] = Post::whereIn("id",$posts)->get()->take($num);
            }else{
                return self::$pool['rec'][$position][$num] = [];
            }

        }
    }


    /**
     * 返回友情链接 品牌推荐 自媒体推荐
     * @param $type
     * @return mixed
     */
    public function getFriend($type,$num=8){
        if(isset(self::$pool['friend'][$type][$num])) return self::$pool['friend'][$type][$num];
        else{
            return self::$pool['friend'][$type][$num] = Friend::where('type',$type)->get()->take($num);
        }
    }

}










